package com.ibm.risk.irmp.base.seq.rest;


import com.ibm.risk.irmp.base.se.service.SeqService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/v2/seq")
public class SeqServiceController {
    @Autowired
    SeqService seqService;

    @GetMapping("/next-val")
    @Operation(summary = "获取唯一系列号", description = "给定系列名，可选一个分组（号段），获取唯一的系列号。如系列名RiskWarn, 号段20240101")
    public int getNextSeq(@RequestParam String seqName, @RequestParam(defaultValue = "") String group) {
        try {
            return seqService.getNextSeq(seqName, group);
        } catch (ObjectOptimisticLockingFailureException e) {
            log.warn("OptimisticLockingFailureException: {}", e.getMessage());
            return getNextSeq(seqName, group);
        }
    }
}
